<template>
  
  <a-modal
    v-model:open="visible"
    :title="null"
    :footer="null"
    width="80%"
    :body-style="{ padding: 0 }"
    class="article-detail-modal"
    @cancel="handleClose"
  >
    <div v-if="currentArticle" class="article-detail">
      <!-- 文章头部 -->
      <div class="article-header relative">
        <!-- 封面图 -->
        <div v-if="currentArticle?.image" class="cover-image relative">
          <img 
            :src="currentArticle?.image" 
            :alt="currentArticle?.title"
            class="w-full h-96 object-cover"
          >
          <div class="absolute inset-0 bg-black bg-opacity-30"></div>
          
          <!-- 关闭按钮 -->
          <a-button 
            shape="circle" 
            type="text" 
            class="absolute top-4 right-4 text-white hover:bg-white hover:bg-opacity-20"
            @click="handleClose"
          >
            <template #icon>
              <CloseOutlined />
            </template>
          </a-button>
        </div>
        
        <!-- 文章信息 -->
        <div class="article-info p-8">
          <div class="flex items-center mb-4">
            <a-avatar :src="currentArticle?.avatar" :size="48" class="mr-3 inline-block" />
              <div class="text-sm text-neutral-500">发布者：{{ currentArticle?.username || '未知用户' }}</div>
          </div>
          
          <h1 class="text-3xl font-bold text-neutral-900 mb-4">{{ currentArticle?.title }}</h1>
          
          <!-- 标签 -->
          <div class="flex flex-wrap gap-2 mb-6">
            <a-tag
              v-for="tag in currentArticle?.tags"
              :key="tag.name"
              :class="tag.color"
            >
              {{ tag.name }}
            </a-tag>
          </div>
          
          <!-- 统计和操作 -->
          <div class="flex items-center justify-between">
            <!-- <div class="flex items-center space-x-6 text-neutral-500">
              <div class="flex items-center">
                <EyeOutlined class="mr-1" />
                <span>{{ currentArticle?.views }}</span>
              </div>
              <div class="flex items-center">
                <CommentOutlined class="mr-1" />
                <span>{{ currentArticle?.comments }}</span>
              </div>
              <div class="flex items-center">
                <HeartOutlined class="mr-1" />
                <span>{{ currentArticle?.likes }}</span>
              </div>
            </div> -->
          </div>
        </div>
      </div>
      
      <!-- 文章内容 -->
      <div class="article-content px-8 pb-8">
        <div class="prose max-w-none">
          <!-- 文章正文 -->
          <!-- <div v-if="currentArticle?.content" v-html="currentArticle?.content" class="text-neutral-700 leading-relaxed text-lg"></div> -->
          <MarkdownRenderer :markdown="currentArticle?.content" :jumpToTime="noop" />
          <!-- 如果没有详细内容，显示扩展的描述 -->
          
        </div>
        <!-- 文章底部互动 -->
        <div class="mt-12 pt-8 border-t border-neutral-200">
          <div class="flex items-center justify-between">
            <div class="flex items-center space-x-4" >

            
                  <a-button shape="round" @click="toggleStar" style="margin-right: 10px;margin-top: 15px;margin-bottom: 15px;" >
                <template #icon>
                  <StarOutlined v-if="!currentArticle?.starred" />
                  <StarFilled v-else class="text-yellow-500" />
                </template>
                {{ currentArticle?.starred ? '已收藏' : '收藏' }}
              </a-button>
              <a-button shape="round"  style="margin-right: 10px;">
                <template #icon>
                  <ShareAltOutlined />
                </template>
                分享
              </a-button>
              <a-button type="primary" shape="round"  style="margin-right: 10px;">
                <template #icon>
                  <HeartOutlined />
                </template>
                点赞 ({{ currentArticle?.likeCount }})
              </a-button>
              <a-button shape="round" style="margin-right: 10px;" >
                <template #icon>
                  <CommentOutlined />
                </template>
                评论 ({{ currentArticle?.commentCount }})
              </a-button>
            
            </div>
            
            <div class="text-neutral-500 text-sm">
              发布于 {{ new Date(currentArticle?.publishTime).toLocaleString() }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script setup>
import { ref, watch,onMounted,computed } from 'vue'
import { message } from 'ant-design-vue'
import MarkdownRenderer from '@/components/MarkdownRenderer.vue'
import {
  CloseOutlined,
  StarOutlined,
  StarFilled,
  ShareAltOutlined,
  EyeOutlined,
  CommentOutlined,
  HeartOutlined
} from '@ant-design/icons-vue'
//子组件使用post的状态机
import { storeToRefs } from 'pinia'
import { usePostStore } from '@/stores/postStore'
const postStore = usePostStore()
const { postList, total } = storeToRefs(postStore)

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  article: {
    type: Object,
    default: null
  },
  articleId: Number
})

const emit = defineEmits(['update:visible', 'star-toggle'])

const visible = ref(props.visible)

watch(() => props.visible, (newVal) => {
  visible.value = newVal
})

watch(visible, (newVal) => {
  emit('update:visible', newVal)
})

const handleClose = () => {
  visible.value = false
}

const currentArticle = computed(() =>
  postList.value.find(item => item.id === props.articleId)
)
const toggleStar = () => {
  if (props.article) {
    emit('star-toggle', props.article.id)
    message.success(props.article.starred ? '已取消收藏' : '已添加到收藏')
  }
}
onMounted(() => {
  console.log('当前文章:', currentArticle.value);
  
})
</script>

<style scoped>
.article-detail-modal :deep(.ant-modal-content) {
  border-radius: 12px;
  overflow: hidden;
}

.article-detail-modal :deep(.ant-modal-body) {
  max-height: 80vh;
  overflow-y: auto;
}

.prose {
  line-height: 1.8;
}

.prose h2 {
  margin-top: 2rem;
  margin-bottom: 1rem;
}

.prose h3 {
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
}

.prose p {
  margin-bottom: 1rem;
}

.prose ul {
  margin-bottom: 1rem;
}

.text-neutral-200 {
  color: #E5E7EB;
}

.text-neutral-500 {
  color: #6B7280;
}

.text-neutral-700 {
  color: #374151;
}

.text-neutral-900 {
  color: #111827;
}

.border-neutral-200 {
  border-color: #E5E7EB;
}

.test a-button {
  padding-left: 30px;
}
</style>